package com.mtf.syncExportExcel.service.impl;

import com.google.common.collect.Lists;
import com.mtf.entity.MtfOrder;
import com.mtf.service.IMtfOrderService;
import com.mtf.syncExportExcel.entity.BaseExport;
import com.mtf.syncExportExcel.entity.EasyExcelExport;
import com.mtf.syncExportExcel.entity.EasyExportEntity;
import com.mtf.syncExportExcel.entity.ExportTaskEntity;
import com.mtf.syncExportExcel.handle.EasyExportHandle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ExportServiceImpl {

    @Autowired
    CommonExcelService commonExcelService;
    @Autowired
    private IMtfOrderService orderService;

    public void export(Long id) {
        // 测试代码
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                EasyExportEntity<MtfOrder, Integer> easyExportBean = new EasyExportEntity<>();
                easyExportBean.setExportId(id);
                easyExportBean.setFileName("test" + System.currentTimeMillis());
                easyExportBean.setSheetName("test");
                easyExportBean.setClz(MtfOrder.class);
                //匿名内部类
                easyExportBean.setHandle(new EasyExportHandle<MtfOrder, Integer>() {
                    @Override
                    public List<MtfOrder> handle(EasyExcelExport.ExportContext<MtfOrder, Integer> exportContext, ExportTaskEntity exportTaskEntity) {
                        List<MtfOrder> list = orderService.list();
                        System.out.println("查询的数据量：" + list.size());
                        return list;
                    }
                });
                commonExcelService.easyExport(easyExportBean);
                log.info("导出子线程结束了...");
            }
        });
        thread.start();
        try {
            thread.join(); // 主线程等待子线程结束
            log.info("打印线程的名称：{}",thread.getName());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("主线程结束");
    }
}
